package _interview150;

import _object.ListNode;

/**
 * 19. 删除链表的倒数第 N 个结点
 */
public class No19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(-1, head);
        ListNode cur = head;
        for (int i = 1; i < n; i++) {
            cur = cur.next;
        }
        ListNode pre = dummy;
        while (cur.next != null) {
            pre = head;
            head = head.next;
            cur = cur.next;
        }
        pre.next = pre.next.next;
        return dummy.next;
    }
}
